import { alertCodePattern } from "components/flowdetailpannel/DuedateSetting";

export const validateTimeoutSettings = (
  alertTimeAfterTimeoutCheck,
  processTimeAfterTimeoutCheck, // 任务超时时间不得超过流程超时时间
  alertCodeDuplicateCheck,
  illegalAlertCodeCheck,
  timeoutSetting,
  processTime, // 流程超时时间
  checkingItem,
  zeroTimeoutCheck, // 超时时间 不得为 0 0 0
  zeroWarningCheck, // 预警时间不得为 0 0 0
) => {
  const {
    day: d = 0,
    hour: h = 0,
    minute: m = 0,
    warningSetting = {},
  } = timeoutSetting;
  const alertCodeMap = {};
  const { warningRegular: alerts = [] } = warningSetting;
  if (timeoutSetting && timeoutSetting.isTimeout === "true") {
    if (d === 0 && h === 0 && m === 0) {
      zeroTimeoutCheck.push(checkingItem);
    }
  }
  // if (warningSetting && warningSetting.isWarning === "true") {

  // }
  if (processTime) {
    const { day: days = 0, hour: hours = 0, minute: minutes = 0 } = processTime;
    if (
      d > days ||
      (d === days && h > hours) ||
      (d === days && h === hours && m >= minutes)
    ) {
      if (d !== 0 || h !== 0 || m !== 0) {
        processTimeAfterTimeoutCheck.push(checkingItem);
      }
    }
  }

  alerts.forEach((alert) => {
    const { day = 0, hour = 0, minute = 0 } = alert;
    if (day === 0 && hour === 0 && minute === 0) {
      zeroWarningCheck.push(checkingItem);
    }
    if (
      day > d ||
      (day === d && hour > h) ||
      (day === d && hour === h && minute >= m)
    ) {
      if (day !== 0 || hour !== 0 || minute !== 0) {
        alertTimeAfterTimeoutCheck.push(checkingItem);
      }
    }
    if (!alertCodePattern.test(alert.warningCode)) {
      illegalAlertCodeCheck.push(checkingItem);
    }
    if (alertCodeMap[alert.warningCode]) {
      alertCodeDuplicateCheck.push(checkingItem);
    }
    alertCodeMap[alert.warningCode] = true;
  });
};

const getDetail = (item) => {
  let content;
  let selectId;
  if (item.label) {
    // 用户任务预警设置格式错误
    content = `"${item.label}"`;
    selectId = item.id;
  } else {
    // 流程预警设置格式错误
    content = "流程";
    selectId = "";
  }
  return { content, selectId };
};

export const getTimeoutErrorInfo = (
  alertCodeDuplicateCheck,
  alertTimeAfterTimeoutCheck,
  processTimeAfterTimeoutCheck,
  illegalAlertCodeCheck,
  zeroProcessTimeoutCheck,
  zeroProcessWarningCheck,
  zeroTimeoutCheck,
  zeroWarningCheck,
) => {
  // if (illegalAlertCodeCheck.length > 0) {
  //   const item = illegalAlertCodeCheck[0];
  //   const { content, selectId } = getDetail(item);
  //   return {
  //     notificationArgs: [
  //       "warning",
  //       "",
  //       `${content}预警编码输入格式不正确，请修改后保存`,
  //     ],
  //     selectId,
  //   };
  // }

  // if (alertCodeDuplicateCheck.length > 0) {
  //   const item = alertCodeDuplicateCheck[0];
  //   const { content, selectId } = getDetail(item);
  //   return {
  //     notificationArgs: ["warning", "", `${content}预警编码重复，请修改后保存`],
  //     selectId,
  //   };
  // }
  if (zeroProcessTimeoutCheck.length > 0) {
    const item = zeroProcessTimeoutCheck[0];
    const { content, selectId } = getDetail(item);
    return {
      notificationArgs: ["warning", "", `${content}超时时间不能设置为 0 0 0`],
      selectId,
    };
  }
  if (zeroProcessWarningCheck.length > 0) {
    const item = zeroProcessWarningCheck[0];
    const { content, selectId } = getDetail(item);
    return {
      notificationArgs: ["warning", "", `${content}预警时间不能设置为 0 0 0`],
      selectId,
    };
  }
  if (zeroTimeoutCheck.length > 0) {
    const item = zeroTimeoutCheck[0];
    const { content, selectId } = getDetail(item);
    return {
      notificationArgs: ["warning", "", `${content}超时时间不能设置为 0 0 0`],
      selectId,
    };
  }
  if (zeroWarningCheck.length > 0) {
    const item = zeroWarningCheck[0];
    const { content, selectId } = getDetail(item);
    return {
      notificationArgs: ["warning", "", `${content}预警时间不能设置为 0 0 0`],
      selectId,
    };
  }
  if (alertTimeAfterTimeoutCheck.length > 0) {
    const item = alertTimeAfterTimeoutCheck[0];
    const { content, selectId } = getDetail(item);
    return {
      notificationArgs: [
        "warning",
        "",
        `${content}预警时间不能设置为超时时间之后，请重新设置`,
      ],
      selectId,
    };
  }
  if (processTimeAfterTimeoutCheck.length > 0) {
    const item = processTimeAfterTimeoutCheck[0];
    const { content, selectId } = getDetail(item);
    return {
      notificationArgs: [
        "warning",
        "",
        `${content}超时时间不能设置为流程超时时间之后，请重新设置`,
      ],
      selectId,
    };
  }
  return null;
};
